package com.example.cat.widgetxuexiaoban.WidgetManger;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RemoteViews;

import com.example.cat.widgetxuexiaoban.R;
import com.example.cat.widgetxuexiaoban.Utils.LogHelper;
import com.example.cat.widgetxuexiaoban.Utils.NewImgAdapter;
import com.example.cat.widgetxuexiaoban.Utils.SharedPreferencesHelper;

/**
 * Implementation of App Widget functionality.
 */
public class NewAppWidget extends AppWidgetProvider {
    static int[] TempappWidgetIds = new int[]{};

    public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {



        for (int TempWidgetId : TempappWidgetIds) {
            {
                LogHelper.PrintLog("TempWidgetId" + TempappWidgetIds);

                Intent intent = new Intent(context, SelectImgActivity.class);
//创建一个PendingIntent
                PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
                // Construct the RemoteViews object
                RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
                views.setOnClickPendingIntent(R.id.widget_img, pending);

                int Imgindex = SharedPreferencesHelper.GetIntValue("WidgetInfo", "ImgIndex");

                LogHelper.PrintLog("WidgetPIC  Index：" + Imgindex);
                views.setImageViewBitmap(R.id.widget_img, new NewImgAdapter().GetBitMap(Imgindex));
                views.setTextColor(R.id.timePicker, Color.WHITE);
                // Instruct the widget manager to update the widget
                appWidgetManager.updateAppWidget(TempWidgetId, views);

            }
        }
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        TempappWidgetIds = appWidgetIds;
        LogHelper.PrintLog("触发Widget更新");
        for (int appWidgetId : appWidgetIds) {

            updateAppWidget(context, appWidgetManager, appWidgetId);

        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onEnabled(Context context) {

        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {

        // Enter relevant functionality for when the last widget is disabled
    }

    @Override
    public void onReceive(Context context, Intent intent) {


        super.onReceive(context, intent);
    }
}

